package com.iceclean.serviceImpl;

import com.iceclean.dao.PostBarDao;
import com.iceclean.dao.PostBarFollowDao;
import com.iceclean.dao.UserDao;
import com.iceclean.daoImpl.PostBarDaoImpl;
import com.iceclean.daoImpl.PostBarFollowDaoImpl;
import com.iceclean.daoImpl.UserDaoImpl;
import com.iceclean.po.*;
import com.iceclean.service.PostBarService;

import java.util.ArrayList;
import java.util.List;

/**
 * @author : Ice'Clean
 * @date : 2021-05-04
 *
 * 贴吧服务实体
 */
public class PostBarServiceImpl implements PostBarService {

    PostBarDao postBarDao = new PostBarDaoImpl();
    PostBarFollowDao postBarFollowDao = new PostBarFollowDaoImpl();
    UserDao userDao = new UserDaoImpl();

    @Override
    public PostBar findPostBarById(int postBarId) {
        return postBarDao.findPostBarById(postBarId);
    }

    @Override
    public PostBar findPostBarByName(String postBarName) {
        return postBarDao.findPostBarByName(postBarName);
    }

    @Override
    public List<PostBar> findPostBarsByPage(int check, int page, int num) {
        if (check == 1) {
            return postBarDao.findPostBarsByPageForUnChecked(page, num);
        }
        return postBarDao.findPostBarsByPageForChecked(page, num);
    }

    @Override
    public List<PostBar> findUserPostBarByPage(int userId, int page, int num) {
        return postBarDao.findUserPostBarByPage(userId, page, num);
    }

    @Override
    public List<PostBar> searchPostBarByPage(String search, int page, int num) {
        return postBarDao.searchPostBarByPage(search, page, num);
    }

    @Override
    public int getAllSearchForNumber(String search) {
        return postBarDao.getAllSearchForNumber(search);
    }

    /**
     * 先获取得到所有的贴吧关注记录
     * 再根据关注记录查询对应的贴吧，存放再集合中并返回
     * @param userId 指定用户的 id
     * @param page 页数
     * @param num 每页的数目
     * @return 符合条件的贴吧集合
     */
    @Override
    public List<PostBar> findPostBarFollowByPage(int userId, int page, int num) {
        // 得到贴吧关注记录列表
        List<PostBarFollow> postBarFollowList = postBarFollowDao.findPostBarFollowByPage(userId, page, num);

        // 取出所有对应的贴吧
        List<PostBar> postBarList = new ArrayList<>();
        for (PostBarFollow postBarFollow : postBarFollowList) {
            postBarList.add(postBarDao.findPostBarById(postBarFollow.getPostBarId()));
        }

        return postBarList.size() == 0 ? null : postBarList;
    }

    @Override
    public int getNumberOfAllPostBar(int check) {
        if (check == 1) {
            return postBarDao.getNumberOfAllPostBarForUnChecked();
        } else {
            return postBarDao.getNumberOfAllPostBarForChecked();
        }
    }

    @Override
    public PostBar createPostBar(int userId, String postBarName, String postBarResume) {
        int id = postBarDao.createPostBar(userId, postBarName, postBarResume);
        return postBarDao.findPostBarById(id);
    }

    /**
     * 先根据贴吧 id 和 用户 id 获取到贴吧对象、贴吧关注对象和用户对象
     * 根据贴吧关注对象是否为空判断该用户是否已经关注该贴吧
     * 未关注则创建关注，要求贴吧的关注数 +1，用户的贴吧关注数也 +1，已关注了则相反
     * @param postBarId 受处理的贴吧 id
     * @param userId 处理的用户 id
     * @return 关注成功返回关注对象，取消关注则返回 null
     */
    @Override
    public PostBarFollow changePostBarFollow(Integer postBarId, Integer userId) {
        // 找到对应的贴吧、贴吧关注对象和用户对象
        PostBar postBar = postBarDao.findPostBarById(postBarId);
        PostBarFollow postBarFollow = postBarFollowDao.findPostBarFollow(postBarId, userId);
        User user = userDao.findUserById(userId);

        // 判断是否已经关注过该贴吧
        if(postBarFollow != null) {
            // 不为空的话取消关注
            // 关注记录取消
            postBarFollowDao.cancelPostBarFollow(postBarFollow);

            // 对应贴吧关注数 -1
            postBar.setPostBarFollow(postBar.getPostBarFollow() - 1);
            postBarDao.updatePostBar(postBar);

            // 对应用户的贴吧关注数 -1
            user.setFollowPostBar(user.getFollowPostBar() - 1);
            userDao.updateUser(user);
            return null;
        }else{
            // 为空的话则新建贴吧关注
            // 新建贴吧关注记录，并重新从数据库获取到详细内容（创建日期）
            int postBarFollowId = postBarFollowDao.createPostBarFollow(new PostBarFollow(postBarId, userId));
            postBarFollow = postBarFollowDao.findPostBarFollowById(postBarFollowId);

            // 对应贴吧关注数 +1
            postBar.setPostBarFollow(postBar.getPostBarFollow() + 1);
            postBarDao.updatePostBar(postBar);

            // 对应用户贴吧关注数 +1
            user.setFollowPostBar(user.getFollowPostBar() + 1);
            userDao.updateUser(user);
            return postBarFollow;
        }
    }

    @Override
    public boolean isUserFollowPostBar(Integer postBarId, Integer userId) {
        return postBarFollowDao.findPostBarFollow(postBarId, userId) != null;
    }

    /**
     * 审核通过时，要将贴吧创建者的贴吧数 +1
     * @param postBarId 审核通过的贴吧 id
     * @return 是否执行成功
     */
    @Override
    public boolean agreeCreatePostBar(int postBarId) {
        // 将贴吧的 check 更新为 1
        PostBar postBar = postBarDao.findPostBarById(postBarId);
        postBar.setPostBarCheck(1);

        // 获取贴吧所属用户对象
        User user = userDao.findUserById(postBar.getUserId());
        user.setUserPostBarNumber(user.getUserPostBarNumber() + 1);
        userDao.updateUser(user);

        return postBarDao.updatePostBar(postBar);
    }

    @Override
    public boolean deletePostBar(int postBarId) {
        return postBarDao.deletePostBar(postBarId);
    }
}
